package me.pcqpcq.android.aclean;

import me.pcqpcq.android.data.PathStorageData;
import me.pcqpcq.android.mgr.ActivityMgr;
import me.pcqpcq.android.msg.MessageCentre;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class CleanMainActivity extends Activity implements OnClickListener {

	private long exitTime = 0;

	private double totalDouble;
	private double availableDouble;

	private ImageView indicatorView;
	private TextView indicatorCapacity;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ActivityMgr.setCurActivity(CleanMainActivity.this);
		setContentView(R.layout.act_clean_main);
		// 构建容量指示器
		buildIndicator();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	/**
	 * 构建外置卡容量指示器
	 */
	private void buildIndicator() {
		// 取得屏幕宽度
		Display display = getWindowManager().getDefaultDisplay();
		int displayWidth = display.getWidth();
		// 指示器前景
		indicatorView = (ImageView) findViewById(R.id.main_indicator_front);
		// 判断外置卡状态
		if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
			PathStorageData extStorage =
					new PathStorageData(Environment.getExternalStorageDirectory().getAbsolutePath());
			// 取得总容量字节
			long totalLong = extStorage.getExternalStorage();
			// 取得可用容量字节
			long availableLong = extStorage.getAvailableStorage();
			// 设置指示器的宽度
			indicatorView.getLayoutParams().width =
					(int) (displayWidth - (int) (displayWidth / ((double) totalLong / (double) availableLong)));

			// 转换容量为自然语言，Double型
			totalDouble = extStorage.getHumanStorage(extStorage.getExternalStorage());
			availableDouble = extStorage.getHumanStorage(extStorage.getAvailableStorage());
			// 生成指示器文本
			String indicatorText = getString(R.string.main_text_indicator, totalDouble - availableDouble, availableDouble, totalDouble);
			indicatorCapacity = (TextView) findViewById(R.id.main_indicator_capacity);
			indicatorCapacity.setText(indicatorText);
		}
	}

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			indicatorCapacity.setText(msg.getData().getString("indicatorText"));
			super.handleMessage(msg);
		}

	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.btn_setting:
			switchToActivity(SettingCleanActivity.class);
			break;

		case R.id.clean_rubbish:
			switchToActivity(CleanRubbishActivity.class);
			break;

		case R.id.clean_apk:
			switchToActivity(CleanApkActivity.class);
			break;

		default:
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if ((System.currentTimeMillis() - exitTime) > 2000) {
				MessageCentre.showShortToast(getString(R.string.toast_exit));
				exitTime = System.currentTimeMillis(); // 通过获取系统时间做判断
			} else {
				finish();
				System.exit(0);
			}
			return true;
		} else
			return super.onKeyDown(keyCode, event);
	}

	private void switchToActivity(Class<?> cls) {
		Intent intent = new Intent();
		intent.setClass(CleanMainActivity.this, cls);
		startActivity(intent);
	}

}